package org.jeecg.modules.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.system.entity.Notice;
import org.jeecg.modules.system.service.INoticeService;
import org.jeecg.modules.system.util.JsonMapper;
import org.jeecg.modules.system.vo.JournalVO;
import org.jeecg.modules.system.vo.ReportWeekVO;
import org.jeecg.modules.system.vo.SystemMsgRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统消息接口
 * @author liurui
 * @date  2017年12月19日上午11:17:54
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/b/app/sysmsg")
public class AppSystemMsgController{

	@Autowired
	private INoticeService noticeService;
	
	
	/**
	 * 取得一级消息种类以及其对应的未读数
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findOneLevelMsgs",method = RequestMethod.POST )
	public Result<?> findOneLevelMsgs(SystemMsgRequestVO msgRequestVO){
		if(StringUtils.isBlank(msgRequestVO.getUserId())){
			return Result.error("参数userId为空");
		}
		//第一层未读消息列表
		List<Notice> msgOneUnLevels = noticeService.findUnReads(msgRequestVO.getUserId());
		//构建消息第一层大分类列表
		List<Notice> msgOneLevels = noticeService.findAllReads(msgRequestVO.getUserId());
		for(Notice bean:msgOneLevels){
			for(Notice unbean:msgOneUnLevels){
				if(bean.getField3() != null && bean.getField3().equals(unbean.getField3())){
					bean.setField7(unbean.getField7());
					break;
				}
			}
		}
		return Result.ok(msgOneLevels);
	}
	
	/**
	 * 根据消息的类型code值取得对应的列表
	 * @param msgRequestVO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findMsgsByType",method = RequestMethod.POST )
	public Result<IPage<Notice>> findMsgsByType(SystemMsgRequestVO msgRequestVO,
												@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												@RequestParam(name="pageSize", defaultValue="10") Integer pageSize){

		if(StringUtils.isBlank(msgRequestVO.getUserId())){
			return Result.error("参数userId为空");
		}
		if(StringUtils.isBlank(msgRequestVO.getMsgType())){
			return Result.error("参数msgType为空");
		}
		IPage<Notice> page = noticeService.selectSqlList(new Page<>(pageNo, pageSize),msgRequestVO);

//		for(Notice bean:page.getRecords()){
//			if("JG001".equals(bean.getNoticeType())){ //日志留言通知
//				JournalVO journal = noticeService.getJournalById(bean.getNoticeParam5());
//				if(journal != null && "0".equals(journal.getDelFlag())){
//					bean.setNoticeParam5(JsonMapper.toJsonString(journal));
//				}
//			} else if("JG002".equals(bean.getNoticeType())){ //周报留言通知
//				ReportWeekVO reportWeek = noticeService.getReportWeekById(bean.getNoticeParam5());
//				if(reportWeek != null && "0".equals(reportWeek.getDelFlag())){
//					bean.setNoticeParam5(JsonMapper.toJsonString(reportWeek));
//				}
//			}
//		}
		return Result.ok(page);
	}
	
	/**
	 * 将消息状态从未读--已读
	 * @param msgRequestVO msgId  消息id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="clearUnRead",method = RequestMethod.POST )
	public Result<?> clearUnRead(SystemMsgRequestVO msgRequestVO){
		if(StringUtils.isBlank(msgRequestVO.getMsgId())){
			return Result.error("参数msgId为空");
		}
		Notice notice = noticeService.getById(msgRequestVO.getMsgId());
		if(notice == null){
			return Result.error("未查询到数据");
		}
		notice.setStatus(Notice.STATUS_ACTED);
		noticeService.updateById(notice);
		return Result.ok("状态更新成功");
	}
	
	/**
	 * 删除消息
	 * @param msgRequestVO msgId 消息id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delMsg",method = RequestMethod.POST )
	public Result<?> delMsg(SystemMsgRequestVO msgRequestVO){
		if(StringUtils.isBlank(msgRequestVO.getMsgId())){
			return Result.error("参数msgId为空");
		}
		boolean resDel = noticeService.removeById(msgRequestVO.getMsgId());
		if(resDel){
			return Result.ok("删除成功");
		}else{
			return Result.error("删除失败");
		}
	}
	
	
	/**
	 * 根据指定消息类型获取未读数  
	 * @param msgRequestVO msgType 消息类型  不传或传空白时表示取当前用户下所有未读消息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findUnreadsByType",method = RequestMethod.POST )
	public Result<?> findUnreadsByType(SystemMsgRequestVO msgRequestVO){
		int unreads = noticeService.findUnreadsByType(msgRequestVO.getUserId(), msgRequestVO.getMsgType());
		return Result.ok(unreads);
	}
	
}
